package com.ruoyi.web.controller.Base;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.domain.Ability_ServiceInfor;
import com.ruoyi.domain.Ability_ServiceLimitingInfor;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.service.Ability_AbiltyService;
import com.ruoyi.service.Ability_ServiceLimitingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/Service")
public class ServiceController extends BaseController {
    @Autowired
    Ability_AbiltyService ability_AbiltyService;
     @Autowired
    Ability_ServiceLimitingService ability_ServiceLimitingService;


    @PostMapping("/add")
    public AjaxResult add(@RequestBody Ability_ServiceInfor ability_ServiceInfor) {
        int k = ability_AbiltyService.insertAbility_ServiceInfor(ability_ServiceInfor);
        String tg[]=ability_ServiceInfor.getLevelgroup();
        if (k > 0) {
             Ability_ServiceInfor infor=  ability_AbiltyService.selectAbility_ServiceInforByName(ability_ServiceInfor.getServiceNameE());
             Ability_ServiceLimitingInfor ability_ServiceLimitingInfor = new Ability_ServiceLimitingInfor();
             ability_ServiceLimitingInfor.setSid(infor.getUUID());
             ability_ServiceLimitingInfor.setSname(infor.getServiceNameE());
             ability_ServiceLimitingInfor.setLevelgroup(tg);
             ability_ServiceLimitingService.insertAbility_ServiceLimitingInfor(ability_ServiceLimitingInfor);
          // ability_ServiceLimitingInfor.setLevelgroup();
            return AjaxResult.success("添加成功");
        }else if(k==-300){
            System.out.println("路由被占用");
            return AjaxResult.warn("对外路由已经被占用");
        }else {
        return AjaxResult.error("添加失败(URL/host格式错误或API服务的英文名已经存在)");}
    }

    @PostMapping("/app")
    public AjaxResult approved(@RequestBody Ability_ServiceInfor ability_ServiceInfor) {
        System.out.println(ability_ServiceInfor.getUUID());
        int k = ability_AbiltyService.ApprovedService(ability_ServiceInfor);
        if (k > 0) {
            return AjaxResult.success("审核成功");
        }

        return AjaxResult.error("审核失败,服务器发生错误，或参数错误，请重新创建该API（服务）");
    }
    @PostMapping("/noapp")
    public AjaxResult noapproved(@RequestBody Ability_ServiceInfor ability_ServiceInfor) {
        System.out.println(ability_ServiceInfor.getUUID());
        int k = ability_AbiltyService.notApprovedService(ability_ServiceInfor);
        if (k > 0) {
            return AjaxResult.success("审核成功--不通过");
        }

        return AjaxResult.error("审核失败");
    }



    @GetMapping("/list")
    public TableDataInfo getServiceList(@RequestParam(required = false,value = "sername") String name,
                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, // 添加默认值可选
                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        startPage();
     Ability_ServiceInfor ability_ServiceInfor = new Ability_ServiceInfor();
     ability_ServiceInfor.setServiceNameE(name);
        List<Ability_ServiceInfor> list = ability_AbiltyService.selectAbility_ServiceInforList(ability_ServiceInfor);
        return getDataTable(list);
    }

    @GetMapping("/del")
    public AjaxResult delete(@RequestParam String id) {
        int k = ability_AbiltyService.deleteAbility_ServiceInforById(id);
        if (k > 0) {
            return AjaxResult.success("删除成功");
        }
        if(k==-100){
            return AjaxResult.warn("该服务已经被使用，无法删除");
        }
        return AjaxResult.error("删除失败");
    }
@PostMapping("/update")
    public AjaxResult update(@RequestBody Ability_ServiceInfor ability_ServiceInfor) {
       int k=0;
        try {
           k= ability_AbiltyService.updateAbility_ServiceInfor(ability_ServiceInfor);
        }catch (Exception e)
            {
                return AjaxResult.error("服务英文名被占用或服务器发生错误");
            }
        if (k==-999){
            return AjaxResult.error("URL/host格式错误");
        }
        if (k==-100){
            return AjaxResult.error("服务已经被审核通过，请到代理服务页面修改相关参数");
        }else if(k==-300){
            return AjaxResult.warn("对外路由已经被占用");
        }

        return AjaxResult.success("修改成功");

    }

    @GetMapping("/select")
    public AjaxResult selectServiceById(@RequestParam String id) {
        return AjaxResult.success(ability_AbiltyService.selectAbility_ServiceInforById(id));
    }
}
